package com.ymatou.promotion.api.testcase;

import java.util.ArrayList;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.promotion.api.parameter.GetUserRetrieveSellerCouponBean;
import com.ymatou.promotion.api.parameter.UserRetrieveSellerCoupon;
import com.ymatou.promotion.api.service.GetUserRetrieveSellerCouponCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymttest.database.model.YmtCouponPrivateUserBound;
import com.ymttest.database.sqlwapper.YmtCouponPrivateUserBoundWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.constant.ECode;

/**
 * 返回用户在所传入的批次id中刷选已经领取过的批次id接口
 *
 * @author chenjiazhu
 */
@TestSuite
public class Ts_GetUserRetrieveSellerCoupon {
    private static GetUserRetrieveSellerCouponBean getuserretrievesellercouponBean;
    private static GetUserRetrieveSellerCouponCall getuserretrievesellercouponCall;

    @BeforeClass
    public static void setUp() {
        Logger.createResultFile("返回用户在所传入的批次id中刷选已经领取过的批次id接口");
    }

    @Before
    public void caseUp() {
        getuserretrievesellercouponBean = new GetUserRetrieveSellerCouponBean();
        getuserretrievesellercouponCall = new GetUserRetrieveSellerCouponCall();
    }

    @AfterClass
    public static void tearDown() {
        Logger.generateResult(getuserretrievesellercouponCall.getOpurl());
    }

    @After
    public void caseDown() {
        Logger.end();
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_GetUserRetrieveSellerCoupon_001() {
        Logger.start(true, "返回用户在所传入的批次id中刷选已经领取过的批次id");
        try {

            setGetUserRetrieveSellerCouponBean();

            getuserretrievesellercouponCall.setData(getuserretrievesellercouponBean);
            getuserretrievesellercouponCall.callService();

            Logger.verifyEquals("200", getuserretrievesellercouponCall.getCode(), "验证返回code");
            Logger.verifyEquals("0", getuserretrievesellercouponCall.getBCode(), "验证返回Bcode");

            String str =
                    "\"UserId\":" + getuserretrievesellercouponBean.getUserId() + ",\"BatchId\":"
                            + getuserretrievesellercouponBean.getBatchIds().get(0);
            Logger.verifyEquals(true, getuserretrievesellercouponCall.getData().contains(str), "验证返回UserId对应BatchId");

            List<UserRetrieveSellerCoupon> userRetrieveSellerCouponList =
                    getuserretrievesellercouponCall.getUserRetrieveSellerCouponList().getUserRetrieveSellerCouponList();

            for (UserRetrieveSellerCoupon userRetrieveSellerCoupon : userRetrieveSellerCouponList) {
                Logger.verifyEquals(getuserretrievesellercouponBean.getUserId(), userRetrieveSellerCoupon.getUserId(),
                        "验证返回UserId正确");
            }

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_GetUserRetrieveSellerCoupon_002() {
        Logger.start(true, "验证UserId空时无返回");
        try {

            List<YmtCouponPrivateUserBound> ymtCouponPrivateUserBounds =
                    new YmtCouponPrivateUserBoundWapper().selectTop5();

            List<Integer> ibatchs = new ArrayList<Integer>();

            for (YmtCouponPrivateUserBound ymtCouponPrivateUserBound : ymtCouponPrivateUserBounds) {
                ibatchs.add(ymtCouponPrivateUserBound.getiBatchId());
            }
            getuserretrievesellercouponBean.setBatchIds(ibatchs);

            getuserretrievesellercouponCall.setData(getuserretrievesellercouponBean);
            getuserretrievesellercouponCall.callService();

            Logger.verifyEquals(ECode.SUCCESS, getuserretrievesellercouponCall.getCode(), "验证返回code");
            Logger.verifyEquals(ECode.BCODE0, getuserretrievesellercouponCall.getBCode(), "验证返回Bcode");

            String str = "{\"UserRetrieveSellerCouponList\":[]}";
            Logger.verifyEquals(str, getuserretrievesellercouponCall.getData(), "验证返回UserId对应BatchId");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_GetUserRetrieveSellerCoupon_003() {
        Logger.start(true, "验证UserId空时无返回");
        try {

            List<YmtCouponPrivateUserBound> ymtCouponPrivateUserBounds =
                    new YmtCouponPrivateUserBoundWapper().selectTop5();
            getuserretrievesellercouponBean.setUserId(ymtCouponPrivateUserBounds.get(0).getiUserId());

            List<Integer> ibatchs = new ArrayList<Integer>();

            for (YmtCouponPrivateUserBound ymtCouponPrivateUserBound : ymtCouponPrivateUserBounds) {
                ibatchs.add(ymtCouponPrivateUserBound.getiBatchId());
            }

            getuserretrievesellercouponCall.setData(getuserretrievesellercouponBean);
            getuserretrievesellercouponCall.callService();

            Logger.verifyEquals(ECode.SUCCESS, getuserretrievesellercouponCall.getCode(), "验证返回code");
            Logger.verifyEquals(ECode.BCODE0, getuserretrievesellercouponCall.getBCode(), "验证返回Bcode");
            Logger.verifyEquals(0, getuserretrievesellercouponCall.getUserRetrieveSellerCouponList()
                    .getUserRetrieveSellerCouponList().size(), "验证返回UserRetrieveSellerCouponList为空");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    @Test
    @Category(P1.class)
    @TestCase
    public void Tc_GetUserRetrieveSellerCoupon_004() {
        Logger.start(true, "验证BatchIds空时返回");
        try {

            getuserretrievesellercouponBean.setUserId(Integer.parseInt(EnvSetup.getData("BuyUserId")));

            getuserretrievesellercouponCall.setData(getuserretrievesellercouponBean);
            getuserretrievesellercouponCall.callService();

            Logger.verifyEquals(ECode.SUCCESS, getuserretrievesellercouponCall.getCode(), "验证返回code");
            Logger.verifyEquals(ECode.BCODE0, getuserretrievesellercouponCall.getBCode(), "验证返回Bcode");

            Logger.verifyEquals("", getuserretrievesellercouponCall.getMsg(), "验证返回UserId对应BatchId");

        } catch (Exception e) {
            Logger.fail(e);
        }
    }

    /**
     * 获取Ymt_CouponPrivateUserBound表中最新5条数据，用5条中的iBatchId以及第一条中的iUserId来设置请求Bean
     */
    private void setGetUserRetrieveSellerCouponBean()
    {
        Logger.comment("获取Ymt_CouponPrivateUserBound表中最新5条数据，用5条中的iBatchId以及第一条中的iUserId来设置请求Bean");

        List<YmtCouponPrivateUserBound> ymtCouponPrivateUserBounds = new YmtCouponPrivateUserBoundWapper().selectTop5();
        getuserretrievesellercouponBean.setUserId(ymtCouponPrivateUserBounds.get(0).getiUserId());

        List<Integer> ibatchs = new ArrayList<Integer>();

        for (YmtCouponPrivateUserBound ymtCouponPrivateUserBound : ymtCouponPrivateUserBounds) {
            ibatchs.add(ymtCouponPrivateUserBound.getiBatchId());
        }

        getuserretrievesellercouponBean.setBatchIds(ibatchs);
    }

}
